package com.chase.passwordmaster.detail

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.chase.passwordmaster.R
import com.chase.passwordmaster.data.AccountViewModel
import com.chase.passwordmaster.detail.edit.EditActivity
import com.chase.passwordmaster.entity.DecryptedAccount
import com.chase.passwordmaster.utils.*

class DetailActivity : AppCompatActivity() {

    companion object {
        fun actionStart(context: Context, accountIndex: Int) {
            val intent = Intent(context, DetailActivity::class.java)
            intent.putExtra("accountIndex", accountIndex)
            context.startActivity(intent)
        }
    }

    private lateinit var accountViewModel: AccountViewModel

    private var accountIndex = -1
    private lateinit var account: DecryptedAccount

    private lateinit var cpBack: View
    private lateinit var tvWebsite: TextView
    private lateinit var tvUsername: TextView
    private lateinit var tvPassword: TextView
    private lateinit var tvName: TextView
    private lateinit var tvTel: TextView
    private lateinit var tvEMail: TextView
    private lateinit var tvQQ: TextView
    private lateinit var tvWeChat: TextView
    private lateinit var tvMemo: TextView

    private lateinit var edit: View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail)

        accountViewModel = ViewModelProviders.of(this).get(AccountViewModel::class.java)

        cpBack = findViewById(R.id.back_detail)
        tvWebsite = findViewById(R.id.tv_website_detail)
        tvUsername = findViewById(R.id.tv_username_detail)
        tvPassword = findViewById(R.id.tv_password_detail)
        tvName = findViewById(R.id.tv_name_detail)
        tvTel = findViewById(R.id.tv_tel_detail)
        tvQQ = findViewById(R.id.tv_qq_detail)
        tvEMail = findViewById(R.id.tv_email_detail)
        tvWeChat = findViewById(R.id.tv_wechat_detail)
        tvMemo = findViewById(R.id.tv_memo_detail)

        edit = findViewById(R.id.edit_detail)

        useDarkStatusBar()
        useImmersingNavigator()

        // Get information from intent
        accountIndex = intent.getIntExtra("accountIndex", -1)

        account = if (accountIndex != -1) {
            accountViewModel.getAccount(accountIndex) ?: DecryptedAccount("error", "error", "error")
        } else DecryptedAccount("error", "error", "error")

        tvWebsite.text = account.website
        tvUsername.text = account.username
        tvPassword.text = account.password

        edit.setOnClickListener {
            EditActivity.actionStart(this, accountIndex)
        }

        accountViewModel.getAccountLiveData().observe(this, Observer {
            account = it[accountIndex]
            account.run {
                tvWebsite.text = website
                tvWebsite.text = website
                tvUsername.text = username
                tvPassword.text = password
                tvName.text = name
                tvTel.text = tel
                tvQQ.text = qq
                tvEMail.text = email
                tvWeChat.text = weChat
                tvMemo.text = memo
            }
        })

        initCPBack(cpBack = cpBack)
        initCPInfo()
    }

    private fun initCPBack(cpBack: View) {
        cpBack.setOnClickListener { onBackPressed() }
    }

    private fun initCPInfo() {
        fun copy(data: String) {
            copy2Clipboard(this, data)
            showToast("已复制到剪贴板")
        }

        val listener = View.OnLongClickListener {
            // Vibrate
            vibrate()
            if (it is TextView) {
                copy(it.text.toString())
            }
            false
        }

        tvWebsite.setOnLongClickListener(listener)
        tvPassword.setOnLongClickListener(listener)
        tvUsername.setOnLongClickListener(listener)
        tvName.setOnLongClickListener(listener)
        tvMemo.setOnLongClickListener(listener)
        tvWeChat.setOnLongClickListener(listener)
        tvEMail.setOnLongClickListener(listener)
        tvQQ.setOnLongClickListener(listener)
        tvTel.setOnLongClickListener(listener)
    }
}